import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import React, { PureComponent } from 'react';
import { Table, Alert } from 'antd';
import intl from "../../utils/intl";
import styles from "./index.less?modules";

function initTotalList(columns) {
  var totalList = [];
  columns.forEach(function (column) {
    if (column.needTotal) {
      totalList.push(_objectSpread(_objectSpread({}, column), {}, {
        total: 0
      }));
    }
  });
  return totalList;
}

var StandardTable = /*#__PURE__*/function (_PureComponent) {
  _inherits(StandardTable, _PureComponent);

  var _super = _createSuper(StandardTable);

  function StandardTable(props) {
    var _this;

    _classCallCheck(this, StandardTable);

    _this = _super.call(this, props);

    _this.handleRowSelectChange = function (selectedRowKeys, selectedRows) {
      var needTotalList = _toConsumableArray(_this.state.needTotalList);

      needTotalList = needTotalList.map(function (item) {
        return _objectSpread(_objectSpread({}, item), {}, {
          total: selectedRows.reduce(function (sum, val) {
            return sum + parseFloat(val[item.dataIndex], 10);
          }, 0)
        });
      });

      if (_this.props.onSelectRow) {
        _this.props.onSelectRow(selectedRows);
      }

      _this.setState({
        selectedRowKeys: selectedRowKeys,
        needTotalList: needTotalList
      });
    };

    _this.handleTableChange = function (pagination, filters, sorter) {
      _this.props.onChange(pagination, filters, sorter);
    };

    _this.cleanSelectedKeys = function () {
      _this.handleRowSelectChange([], []);
    };

    var columns = props.columns;

    var _needTotalList = initTotalList(columns);

    _this.state = {
      selectedRowKeys: [],
      needTotalList: _needTotalList
    };
    return _this;
  }

  _createClass(StandardTable, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: //  eslint-disable-next-line
    function UNSAFE_componentWillReceiveProps(nextProps) {
      // clean state
      var _nextProps$selectedRo = nextProps.selectedRows,
          selectedRows = _nextProps$selectedRo === void 0 ? {} : _nextProps$selectedRo,
          _nextProps$columns = nextProps.columns,
          columns = _nextProps$columns === void 0 ? [] : _nextProps$columns;

      if (selectedRows.length === 0) {
        var needTotalList = initTotalList(columns);
        this.setState({
          selectedRowKeys: [],
          needTotalList: needTotalList
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
          selectedRowKeys = _this$state.selectedRowKeys,
          needTotalList = _this$state.needTotalList;
      var _this$props = this.props,
          _this$props$data = _this$props.data,
          data = _this$props$data === void 0 ? {} : _this$props$data,
          loading = _this$props.loading,
          columns = _this$props.columns,
          rowKey = _this$props.rowKey,
          handleSelectRows = _this$props.handleSelectRows,
          _this$props$showTable = _this$props.showTableAlert,
          showTableAlert = _this$props$showTable === void 0 ? false : _this$props$showTable;
      var _data$list = data.list,
          list = _data$list === void 0 ? [] : _data$list,
          pagination = data.pagination;

      var paginationProps = _objectSpread({
        showSizeChanger: true,
        showQuickJumper: true
      }, pagination);

      var rowSelection = {
        selectedRowKeys: selectedRowKeys,
        onChange: this.handleRowSelectChange,
        getCheckboxProps: function getCheckboxProps(record) {
          return {
            disabled: record.disabled
          };
        }
      };
      return /*#__PURE__*/React.createElement("div", {
        className: styles.standardTable
      }, handleSelectRows ? handleSelectRows(needTotalList) : showTableAlert ? /*#__PURE__*/React.createElement("div", {
        className: styles.tableAlert
      }, /*#__PURE__*/React.createElement(Alert, {
        message: /*#__PURE__*/React.createElement(React.Fragment, null, intl.get('cpaas.common.components.standardTable.select').d('已选择'), ' ', /*#__PURE__*/React.createElement("a", {
          style: {
            fontWeight: 600
          }
        }, selectedRowKeys.length), ' ', intl.get('cpaas.common.components.standardTable.item').d('项'), "\xA0\xA0", needTotalList.map(function (item) {
          return /*#__PURE__*/React.createElement("span", {
            style: {
              marginLeft: 8
            },
            key: item.dataIndex
          }, item.title, intl.get('cpaas.common.components.standardTable.atAll').d('总计'), "\xA0", /*#__PURE__*/React.createElement("span", {
            style: {
              fontWeight: 600
            }
          }, item.render ? item.render(item.total) : item.total));
        }), /*#__PURE__*/React.createElement("a", {
          onClick: this.cleanSelectedKeys,
          style: {
            marginLeft: 24
          }
        }, intl.get('cpaas.common.button.clear').d('清空'))),
        type: "info",
        showIcon: true
      })) : null, /*#__PURE__*/React.createElement(Table, {
        loading: loading,
        rowKey: rowKey || 'key',
        rowSelection: rowSelection,
        dataSource: list,
        columns: columns,
        size: "small",
        pagination: paginationProps,
        onChange: this.handleTableChange
      }));
    }
  }]);

  return StandardTable;
}(PureComponent);

StandardTable.defaultProps = {
  onChange: function onChange(e) {
    return e;
  }
};
StandardTable.defaultProps = {
  onChange: function onChange(e) {
    return e;
  }
};
export default StandardTable;